//
//  MyViewController.m
//  QXShowing
//
//  Created by 王道道 on 2018/4/11.
//  Copyright © 2018年 doll_dream. All rights reserved.
//

#define headPortrait_Height 180 *SizeScale
#define footPortrait_Height 80 *SizeScale


#import "MyViewController.h"

#import "MineHeaderView.h"
#import "MineFooterView.h"
#import "MineTableViewCell.h"

#import "MyInfoViewController.h"//我的信息
//#import "ApplyShowViewController.h"//展位申请
#import "NewApplyShowViewController.h"
#import "MyShowViewController.h"//我的展位
#import "ChatServiceViewController.h"//联系官方
#import "MyCodeViewController.h"//我的推广二维码
#import "MyMarkViewController.h"//我的足迹

#import "NewLoginViewController.h"

#import "UIButton+ClickAction.h"


static NSString *const mineCell = @"MineCell";


@interface MyViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate, BaseProtocol>

@property (nonatomic, strong) RefreshTableView *tableView;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) MineHeaderView *headerView;
@property (nonatomic, strong) MineFooterView *footerView;

@property (nonatomic, strong) SGUserModel *userModel;

@property (nonatomic, strong) NSString *kefuPhone;

@end

@implementation MyViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.userModel = [SGUserInfo getUserInfo];
    [self setUpView];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

//- (void)viewWillDisappear:(BOOL)animated {
//    [super viewWillDisappear:animated];
//    [self.navigationController setNavigationBarHidden:NO animated:YES];
//}

#pragma mark -
- (MineHeaderView *)headerView {
    if (_headerView == nil) {
        _headerView = [[MineHeaderView alloc]initWithFrame:CGRectMake(0, 0, App_Frame_Width, headPortrait_Height)];
        _headerView.baseProtocol = self;
    }
    return _headerView;
}

- (MineFooterView *)footerView {
    if (_footerView == nil) {
        _footerView = [[MineFooterView alloc] initWithFrame:CGRectMake(0, 0, App_Frame_Width, footPortrait_Height)];
        _footerView.baseProtocol = self;
    }
    return _footerView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initNavBar:@"个人中心"];
    
    self.dataArray = @[@"展位申请",@"我的展位",@"联系官方",@"我的足迹",@"我的推广二维码"];

    [self setUpUI];
    
    [self requestPostBH_Ads_getKefuNumber];
    
//    self.userModel = [SGUserInfo getUserInfo];
//    self.headerView.nameLabel.text = [BHSettingUtil dataAndStringIsNull:self.userModel.company_name]?@"去登录":self.userModel.company_name;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)setUpUI {
    self.tableView = [[RefreshTableView alloc]initWithFrame:CGRectMake(0, kStatusHeight, App_Frame_Width, App_Frame_Height - kBottomBarHeight-kStatusHeight) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//    self.tableView.mj_header = refreshHeader;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.tableHeaderView = [[UIView alloc]init];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.backgroundColor = Bg_Color_F9F9F9;
    [self.view addSubview:self.tableView];
    
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, App_Frame_Width, headPortrait_Height)];
    [backView addSubview:self.headerView];
    self.tableView.tableHeaderView = backView;
    
    UIView *backView1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, App_Frame_Width, footPortrait_Height)];
    [backView1 addSubview:self.footerView];
    self.tableView.tableFooterView = backView1;
}

- (void)setUpView {
//    if ([self.userModel.is_agent integerValue] == 0) {
//        //推广员
//        self.dataArray = @[@"联系官方",@"我的推广二维码"];
//    }
//    else if ([self.userModel.state integerValue] == 1) {
//        //展商
//        self.dataArray = @[@"展位申请",@"我的展位",@"联系官方"];
//    }
//    else if ([self.userModel.state integerValue] == 0) {
//        //
//        self.dataArray = @[@"联系官方"];
//    }
//    else {
//        //
        self.dataArray = @[@"展位申请",@"我的展位",@"联系官方",@"我的足迹",@"我的推广二维码"];
//    }
    [self.tableView reloadData];
    self.headerView.nameLabel.text = [BHSettingUtil dataAndStringIsNull:self.userModel.company_name]?@"未登录":self.userModel.company_name;
    [self.footerView.nameButton setTitle:[BHSettingUtil dataAndStringIsNull:self.userModel.company_name]?@"请登录":@"退出登录" forState:UIControlStateNormal];

}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mineCell];
    if (cell == nil) {
        cell = [[MineTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:mineCell];
    }
    cell.contentLabel.text = self.dataArray[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//    cell.contentImage.image = [UIImage imageNamed:self.imageArray[indexPath.section][indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60 * SizeScale;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (!self.isLogin) return;
    SGUserModel *model = [SGUserInfo getUserInfo];
    if ([BHSettingUtil dataAndStringIsNull:model.company_id]) {
        [[BHAlertUtil alertManager] showPromptInfo:@"请先登录～"];
    }
    else {
//        [self.dataArray[indexPath.row] isEqualToString:@"展位申请",@"我的展位",@"联系官方",@"我的推广二维码"]
        
        if ([self.dataArray[indexPath.row] isEqualToString:@"展位申请"]) {
            //        ApplyShowViewController *next = [[ApplyShowViewController alloc] init];
            NewApplyShowViewController *next = [[NewApplyShowViewController alloc] init];
            [self pushViewController:next animated:YES];
        } else if ([self.dataArray[indexPath.row] isEqualToString:@"我的展位"]) {
            MyShowViewController *next = [[MyShowViewController alloc] init];
            [self pushViewController:next animated:YES];
        } else if ([self.dataArray[indexPath.row] isEqualToString:@"联系官方"]) {
//            ChatServiceViewController *next = [[ChatServiceViewController alloc] init];
//            [self pushViewController:next animated:YES];
            
            if (![BHSettingUtil dataAndStringIsNull:self.kefuPhone]) {
                [self callClick];
            }
            else {
                [[BHAlertUtil alertManager] showPromptInfo:@"暂无官方电话，sorry～"];
            }
        } else if ([self.dataArray[indexPath.row] isEqualToString:@"我的足迹"]) {
            MyMarkViewController *next = [[MyMarkViewController alloc] init];
            [self pushViewController:next animated:YES];
        } else if ([self.dataArray[indexPath.row] isEqualToString:@"我的推广二维码"]) {
            if ([BHSettingUtil dataAndStringIsNull:model.qrcode]) {
                [[BHAlertUtil alertManager] showPromptInfo:@"您还没有推广二维码～"];
            } else {
                MyCodeViewController *next = [[MyCodeViewController alloc] init];
                [self pushViewController:next animated:YES];
            }
        } else {
            
        }
    }
}

- (void)logoutProtocol {
//    NewLoginViewController *next = [[NewLoginViewController alloc] init];
//    [self pushViewController:next animated:YES];
    
    self.userModel = [SGUserInfo getUserInfo];
    SGUserModel *model = [[SGUserModel alloc] init];
    model.UserPhone = self.userModel.UserPhone;
    [SGUserInfo saveUserInfo:model];
    
//    SGUserModel *userModel = [[SGUserModel alloc] init];
//    [SGUserInfo saveUserInfo:userModel];
    [(AppDelegate *)[UIApplication sharedApplication].delegate gotoLogin:nil];
}

- (void)loginProtocol {
    if ([BHSettingUtil dataAndStringIsNull:self.userModel.Id]) {
        [(AppDelegate *)[UIApplication sharedApplication].delegate gotoLogin:nil];
    }
}

- (void)callClick {
//    NSString *phone = @"10086";
    
    NSMutableString * phoneStr=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",self.kefuPhone];
    NSURL *phoneUrl = [NSURL URLWithString:phoneStr];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        if ([[UIApplication sharedApplication] openURL:phoneUrl]) {
            [[UIApplication sharedApplication] openURL:phoneUrl];
        }
        else {
            [[BHAlertUtil alertManager] showPromptInfo:@"拨打电话失败"];
        }
    });
//    [sender callAction:@"10086"];

//    if (![BHSettingUtil dataAndStringIsNull:self.dataModel.phone]) {
//        [sender callAction:self.dataModel.phone];
//    }
}

#pragma mark - BHTellProtocol
//- (void)tellRefreshProtocol:(UIViewController *)vc isRefresh:(BOOL)isRefresh {
//    if (isRefresh) {
//        [self requestGetMeInfo];
//    }else {
//        self.personalModel = nil;
////        self.navigationView.nameLabel.text = @"";
//        self.headerView.infoModel = self.personalModel;
//        [self.tableView reloadData];
//    }
//}
//
//- (void)loginRegisterClick:(NSNotification *)notification {
//    NSDictionary *dic = notification.userInfo;
//    LoginRegisterProtocolType protocolType = [dic[BHLoginRegister_Key] integerValue];
//    if (protocolType == LoginRegisterProtocolTypeBack || protocolType == LoginRegisterProtocolTypeLogOut) {
//        self.personalModel = nil;
//        self.headerView.infoModel = self.personalModel;
//        [self.tableView reloadData];
//    }else {
//        [self requestGetMeInfo];
//    }
//}
//
//
//#pragma mark - 接口
//- (void)loadData:(id)result {
//    self.personalModel = [PersonalInfoModel yy_modelWithJSON:result];
//    self.headerView.infoModel = self.personalModel;
////    self.navigationView.nameLabel.text = self.personalModel.company_name;
//    [self.tableView reloadData];
//}
//
//- (void)requestGetMeInfo {
//    SGUserModel *userModel = [SGUserInfo getUserInfo];
//    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    [params setObject:[BHSettingUtil dataAndStringIsNull:userModel.Token] ? @"" : userModel.Token forKey:@"Token"];
//    [PPHTTPRequest requestGetMeInfo:^(id response, BOOL success) {
//        if (success) {
//            [self loadData:response];
//        }else {
//            SGUserModel *userModel = [SGUserInfo getUserInfo];
//            userModel.Token = @"";
//            [SGUserInfo saveUserInfo:userModel];
//        }
//        [self.tableView.mj_header endRefreshing];
//        [[BHAlertUtil alertManager]hiddenLoading];
//    } params:params];
//}

#pragma mark - 联系官方
- (void)requestPostBH_Ads_getKefuNumber {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    [params setObject:[BHSettingUtil dataAndStringIsNull:userModel.Token] ? @"" : userModel.Token forKey:@"Token"];
    [PPHTTPRequest requestPostBH_Ads_getKefuNumber:^(id response, BOOL success) {
        if (success) {
            self.kefuPhone = response[@"phone"];
        }else {
        }
    } params:params];
}
@end
